#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.6)
project(NodeCpp)

set(NodeCpp_Root ${PROJECT_SOURCE_DIR})

#------------------------------------------------------------------------------
# 查找依赖
include(cmake/Dependencies.cmake)

#------------------------------------------------------------------------------
# 工程设置
if (UNIX)
  add_definitions(-Wall)
  if (CMAKE_COMPILER_IS_GNUCXX)
    add_definitions(-std=gnu++0x)
  else ()
    add_definitions(-std=stdc++11)
  endif()
endif()
add_definitions(-D_BUILD)

# 控制台线程同步（防止多线程输出紊乱）
option(CONSOLE_LOCK "Use thread-sync in console." TRUE)
if (NOT CONSOLE_LOCK)
add_definitions(-D_NODECPP_UNLOCK_CONSOLE)
endif()

#------------------------------------------------------------------------------
# 配置工程
include(cmake/CheckSystemPlatform.cmake)
CHECK_SYSTEM_PLATFORM(SYSTEM_PLATFORM SYSTEM_ARCH SYSTEM_ENDIAN)
configure_file(Config.h.in ${NodeCpp_Root}/NodeCpp/Config.h)

if (WIN32)
  set(SRC_PLAT "WIN32")
elseif (UNIX)
  set(SRC_PLAT "POSIX")
endif()

#------------------------------------------------------------------------------
# 设置头文件路径
include_directories(
  "${PROJECT_SOURCE_DIR}"
)

#------------------------------------------------------------------------------
# 工程源文件
set(NodeCpp_Foundation_SRC
  NodeCpp/Platform.h
  NodeCpp/AutoPtr.h
  NodeCpp/ScopedPtr.h
  NodeCpp/ScopedArray.h
  NodeCpp/AtExit.h
  NodeCpp/Macros.h
  NodeCpp/Mutex.h
  NodeCpp/Mutex_${SRC_PLAT}.h
  NodeCpp/LockGuard.h
  NodeCpp/Event.h
  NodeCpp/Event_${SRC_PLAT}.h
  NodeCpp/Thread.h
  NodeCpp/Thread_${SRC_PLAT}.h
  NodeCpp/Runnable.h
  NodeCpp/ThreadPool.h
  NodeCpp/CreatePolicy.h
  NodeCpp/LifeTimePolicy.h
  NodeCpp/ThreadingModel.h
  NodeCpp/Singleton.h
  NodeCpp/Error.h
  NodeCpp/AtomicValue.h
  NodeCpp/String.h
  NodeCpp/Configuration.h
  NodeCpp/ByteConverter.h
  NodeCpp/ByteBuffer.h
  NodeCpp/ErrorHandler.h
  NodeCpp/Time.h
  NodeCpp/LockedQueue.h
  NodeCpp/Path.h
  NodeCpp/Console.h
  NodeCpp/Console_${SRC_PLAT}.h
  NodeCpp/Random.h
  
  src/AtExit.cc
  src/Mutex.cc
  src/Mutex_${SRC_PLAT}.cc
  src/Event.cc
  src/Event_${SRC_PLAT}.cc
  src/Thread.cc
  src/Thread_${SRC_PLAT}.cc
  src/ThreadPool.cc
  src/Error_${SRC_PLAT}.cc
  src/String.cc
  src/String_${SRC_PLAT}.cc
  src/ByteBuffer.cc
  src/Time_${SRC_PLAT}.cc
  src/Path.cc
  src/Console_${SRC_PLAT}.cc
  src/Random.cc
)

set(NodeCpp_Net_SRC
  NodeCpp/Net/SocketApi.h
  NodeCpp/Net/TcpAcceptor.h
  NodeCpp/Net/TcpServer.h
  NodeCpp/Net/TcpSocket.h
  NodeCpp/Net/TcpSocketDelegate.h
  NodeCpp/Net/TcpSocketDelegateFactory.h
  
  src/Net/SocketApi.cc
  src/Net/TcpAcceptor.cc
  src/Net/TcpServer.cc
  src/Net/TcpSocket.cc
)

set(NodeCpp_Crypto_SRC
  NodeCpp/Crypto/BigNumber.h
  NodeCpp/Crypto/Hmac.h
  NodeCpp/Crypto/Sha1.h
  NodeCpp/Crypto/SRP6.h
  
  src/Crypto/BigNumber.cc
  src/Crypto/Hmac.cc
  src/Crypto/Sha1.cc
  src/Crypto/SRP6.cc
)

set(NodeCpp_Util_SRC
  NodeCpp/Util/Option.h
  NodeCpp/Util/OptionSet.h
  NodeCpp/Util/Application.h
  NodeCpp/Util/XmlConfiguration.h
  
  src/Util/Option.cc
  src/Util/OptionSet.cc
  src/Util/Application.cc
  src/Util/XmlConfiguration.cc
)

set(NodeCpp_Data_SRC
  NodeCpp/Data/Rel/Callbacks.h
  NodeCpp/Data/Rel/Field.h
  NodeCpp/Data/Rel/QueryResult.h
  NodeCpp/Data/Rel/Session.h
  NodeCpp/Data/Rel/SessionImpl.h
  NodeCpp/Data/Rel/SqlConnection.h
  NodeCpp/Data/Rel/SqlConnectionFactory.h
  NodeCpp/Data/Rel/SqlConnectionFactoryImpl.h
  NodeCpp/Data/Rel/SqlDelayThread.h
  NodeCpp/Data/Rel/SqlOperation.h
  NodeCpp/Data/Rel/SqlPreparedStatement.h
  
  NodeCpp/Data/Rel/Details/MySQLConnection.h
  NodeCpp/Data/Rel/Details/MySQLConnectionFactoryImpl.h
  NodeCpp/Data/Rel/Details/MySQLConnector.h
  NodeCpp/Data/Rel/Details/MySQLQueryResult.h
  
  
  src/Data/Rel/Field_${SRC_PLAT}.cc
  src/Data/Rel/Field.cc
  src/Data/Rel/QueryResult.cc
  src/Data/Rel/Session.cc
  src/Data/Rel/SqlConnection.cc
  src/Data/Rel/SqlConnectionFactory.cc
  src/Data/Rel/SqlDelayThread.cc
  src/Data/Rel/SqlOperation.cc
  src/Data/Rel/SqlPreparedStatement.cc
  
  src/Data/Rel/Details/MySQLConnection_${SRC_PLAT}.cc
  src/Data/Rel/Details/MySQLConnection.cc
  src/Data/Rel/Details/MySQLConnectionFactoryImpl.cc
  src/Data/Rel/Details/MySQLConnector.cc
  src/Data/Rel/Details/MySQLQueryResult.cc
)

if (WIN32)
  source_group("Foundation" FILES ${NodeCpp_Foundation_SRC})
  source_group("Net" FILES ${NodeCpp_Net_SRC})
  source_group("Crypto" FILES ${NodeCpp_Crypto_SRC})
  source_group("Util" FILES ${NodeCpp_Util_SRC})
  source_group("Data" FILES ${NodeCpp_Data_SRC})
endif()

#------------------------------------------------------------------------------
# 生成目标
add_library(NodeCpp STATIC 
  ${NodeCpp_Foundation_SRC}
  ${NodeCpp_Net_SRC}
  ${NodeCpp_Crypto_SRC}
  ${NodeCpp_Util_SRC}
  ${NodeCpp_Data_SRC}
)